package bdddaosimpl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Set;

import bdd.beans.DBLink;
import bdd.beans.Periodes;
import bdd.beans.Promotion;
import bdddaos.*;
import bdddaos.DAOPromotion;

public class DAOPromotionImpl implements DAOPromotion {

	private DBLink dblink;

	public DAOPromotionImpl() {		
		super();
		this.dblink = new DBLink(); 
	}
	
	@Override
	public Promotion save(Promotion pr) {
		try {
			dblink.connexion();
			this.dblink.setPst(this.dblink.getConnection().prepareStatement("INSERT INTO PROMOTION (numformation)" +
					" VALUES ("+pr.getFormation().getNumformation()+")"));
			this.dblink.getPst().executeQuery();
			DAOOutils outil = new DAOOutils();
			pr.setNumpromotion(outil.findMaxId("PROMOTION", "NUMPROMOTION"));
		}
		catch(SQLException se) {
			System.out.println("Erreur de requ�te");
		}
		finally{
			dblink.deconnexion();
		}
		return pr;
	}

	@Override
	public Promotion find(Integer id) {
		Promotion unePromotion = new Promotion();
		unePromotion.setNumpromotion(id);
		try {
			dblink.connexion();
			this.dblink.setPst(this.dblink.getConnection().prepareStatement("SELECT * FROM PROMOTION WHERE NUMPROMOTION = " + id));
			ResultSet rs = this.dblink.getPst().executeQuery();
			while(rs.next()) {
				unePromotion.setFormation(new DAOFormationImpl().find(rs.getInt("NUMFORMATION")));
			}
		}
		catch(SQLException se) {
			System.out.println("Erreur de requ�te");
		}
		finally{
			dblink.deconnexion();
		}
		return unePromotion;
	}

	@Override
	public List<Promotion> findAll() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void update(Promotion transientObject) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void delete(Promotion persistentObject) {
		try {
			dblink.connexion();
			this.dblink.setPst(this.dblink.getConnection().prepareStatement("DELETE FROM PROMOTION WHERE NUMPROMOTION = " + persistentObject.getNumpromotion()));
		} catch (SQLException e) {
			e.printStackTrace();
		}
		finally{
			dblink.deconnexion();
		}
	}

	@Override
	public Set<Promotion> findByEtudiant(Integer numEtudiant) {
		// TODO Auto-generated method stub
		return null;
	}

}
